package com.glodon.ggzy.fwpt.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.Constants;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.entity.GgZfBulletin;
import com.glodon.ggzy.entity.GgZfBuyregform;
import com.glodon.ggzy.entity.GgZfContract;
import com.glodon.ggzy.entity.GgZfLaw;
import com.glodon.ggzy.entity.GgZfOrder;
import com.glodon.ggzy.entity.GgZfOrginfo;
import com.glodon.ggzy.fwpt.dao.IGgZfBulletinDao;
import com.glodon.ggzy.fwpt.dao.IGgZfBuyregformDao;
import com.glodon.ggzy.fwpt.dao.IGgZfContractDao;
import com.glodon.ggzy.fwpt.dao.IGgZfIezcBulletinDao;
import com.glodon.ggzy.fwpt.dao.IGgZfLawDao;
import com.glodon.ggzy.fwpt.dao.IGgZfOrderDao;
import com.glodon.ggzy.fwpt.dao.IGgZfOrginfoDao;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.webservice.Unit;

/**
 * 
 * 政府采购处理Service实现类
 * @author yangjf
 * @date 2013/06/11
 */
@Service
public class ZfcgProjectServiceImpl implements IZfcgProjectService {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IGgZfIezcBulletinDao ggZfIezcBulletinDao;

	@Autowired
	private IGgZfBuyregformDao ggbugregDao;

	@Autowired
	private IGgZfOrderDao ggZfOrderDao;

	@Autowired
	private IGgZfOrginfoDao orginfoDao;

	@Autowired
	private IGgZfBulletinDao bulletinDao;

	@Override
	public Page queryPage(PageSearchModel queryModel) {
		return ggZfIezcBulletinDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Autowired
	private IGgZfContractDao ggZfContractDao;

	/* 查询合同
	 * type：供应商，采购人
	 */
	@Override
	public Page queryContractPage(PageSearchModel queryModel, String type, String noOrName, HttpServletRequest request) {

		//		Assert.isNull(type,"type不能为空");

		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgZfContract.class);
		if (StringUtils.isNotEmpty(noOrName)) {
			try {
				queryModel.addParamToMap("contractcode_like|contractname_like", "%" + URLDecoder.decode(noOrName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		//		if(type.equals(Constants.ZF_GYS)){
		//			queryModel.addParamToMap("sellerid", getUserOrganId(request));
		//		}else if(type.equals(Constants.ZF_CGR)){
		//			queryModel.addParamToMap("buyerid", getUserOrganId(request));
		//		}else{
		//			queryModel.addParamToMap("sellerid", "$no!ne$");
		//		}

		return ggZfContractDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public Page queryBuyregPage(PageSearchModel queryModel, String noOrName, HttpServletRequest request) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgZfBuyregform.class);
		queryModel.addParamToMap(QuerySupport.ORDERBYDESC, "createtime");
		if (StringUtils.isNotEmpty(noOrName)) {
			try {
				queryModel.addParamToMap("regformcode_like|regformname_like", "%" + URLDecoder.decode(noOrName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		//		if(StringUtils.isNotEmpty(this.getUserOrganId(request))){
		//			queryModel.addParamToMap("buyerid", this.getUserOrganId(request));
		//		}else{
		//			return null;
		//		}
		return ggbugregDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public Page queryOrderPage(PageSearchModel queryModel, String noOrName, Boolean isCgr, HttpServletRequest request) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgZfOrder.class);
		queryModel.addParamToMap(QuerySupport.ORDERBYDESC, "createtime");
		if (StringUtils.isNotEmpty(noOrName)) {
			try {
				queryModel.addParamToMap("ordercode_like|ordername_like", "%" + URLDecoder.decode(noOrName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		//		if(StringUtils.isNotEmpty(this.getUserOrganId(request))){
		//			if(isCgr){
		//				queryModel.addParamToMap("buyerid",this.getUserOrganId(request));
		//			}else{
		//				queryModel.addParamToMap("sellerid",this.getUserOrganId(request));
		//			}
		//		}
		return ggZfOrderDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	public String getUserOrganId(HttpServletRequest request) {
		String organId = (String) request.getAttribute(Constants.ORGANID);
		if (StringUtils.isEmpty(organId)) {
			Unit unit = (Unit) request.getAttribute(Constants.user_session_key);
			if (unit != null && StringUtils.isNotEmpty(unit.getOrganizationCode())) {
				GgZfOrginfo orginfo = (GgZfOrginfo) orginfoDao.findBy("orgcode", unit.getOrganizationCode());
				if (orginfo != null) {
					request.setAttribute(Constants.ORGANID, orginfo.getId());
					return orginfo.getId();
				}
			}
		}
		return organId;
	}

	@Override
	public Page queryZfcgTenderNotice(PageSearchModel queryModel, String name, String type) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgZfBulletin.class);
		queryModel.addParamToMap(QuerySupport.ORDERBYDESC, "createdate");
		if (!StringUtils.isEmpty(name)) {
			queryModel.addParamToMap("bulletinname_like", "%" + name + "%");
		}

		if (Constants.FWPT_ZBGG.equals(type)) {
			queryModel.addParamToMap("block", "systemtype in ('C')");
		}
		if (Constants.FWPT_ZBGS.equals(type)) {
			queryModel.addParamToMap("block", "systemtype in ('Y','Z')");
		}
		if (Constants.FWPT_XXGK.equals(type)) {
			queryModel.addParamToMap("block", "systemtype in ('C','Y','Z')");
		}

		return bulletinDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public GgZfBulletin getGgZfBulletinById(String id) {
		return bulletinDao.get(id);
	}

	@Autowired
	private IGgZfLawDao lawDao;

	/**
	 * 政府采购-法律法规
	 * @param queryModel
	 * @param lawName
	 * @return
	 */
	@Override
	public Page queryZfcgLaw(PageSearchModel queryModel, String lawName) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, GgZfLaw.class);
		queryModel.addParamToMap(QuerySupport.ORDERBYDESC, "updatetime");
		if (!StringUtils.isEmpty(lawName)) {
			queryModel.addParamToMap("name_like", "%" + lawName + "%");
		}
		return lawDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public String getCgrSuccessTransNumber(HttpServletRequest request) {
		StringBuffer quyerHql = new StringBuffer();
		quyerHql.append("select count(*) from GgZfContract");
		if (StringUtils.isNotEmpty(this.getUserOrganId(request))) {
			quyerHql.append(" where buyerid = " + this.getUserOrganId(request));
		}
		List<String> cgr = ggZfContractDao.find(quyerHql.toString());
		return cgr.toString();
	}

	@Override
	public GgZfLaw getGgZfLawById(String id) {
		return lawDao.get(id);
	};

	@Override
	public String getCgrContractAmountNumber(HttpServletRequest request) {
		StringBuffer quyerHql = new StringBuffer();
		quyerHql.append("select sum(contractsum) from GgZfContract");
		if (StringUtils.isNotEmpty(this.getUserOrganId(request))) {
			quyerHql.append(" where buyerid = " + this.getUserOrganId(request));
		}
		List<String> cgr = ggZfContractDao.find(quyerHql.toString());
		return cgr.toString();
	}

	@Override
	public String getGysSuccessTransNumber(HttpServletRequest request) {
		StringBuffer quyerHql = new StringBuffer();
		quyerHql.append("select count(*) from GgZfContract");
		if (StringUtils.isNotEmpty(this.getUserOrganId(request))) {
			quyerHql.append(" where sellerid = " + this.getUserOrganId(request));
		}
		List<String> cgr = ggZfContractDao.find(quyerHql.toString());
		return cgr.toString();
	}

	@Override
	public String getGysContractAmountNumber(HttpServletRequest request) {
		StringBuffer quyerHql = new StringBuffer();
		quyerHql.append("select sum(contractsum) from GgZfContract");
		if (StringUtils.isNotEmpty(this.getUserOrganId(request))) {
			quyerHql.append(" where sellerid = " + this.getUserOrganId(request));
		}
		List<String> cgr = ggZfContractDao.find(quyerHql.toString());
		return cgr.toString();
	}
}
